package com.cheer.ba.utils;

import android.util.Xml;
import com.cheer.ba.model.CityModel;
import com.cheer.ba.model.DistrictsModel;
import com.cheer.ba.model.ProvinceModel;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * pull解析-xml
 *
 * @author HQ
 *         created at 2017/5/15 0015 11:25
 */
public class PullAnalyzeParser implements AnalyzeParser {

    @Override
    public List<ProvinceModel> provinces(InputStream is) throws Exception {
        List<ProvinceModel> provinces = null;
        ProvinceModel province = null;
        //由android.util.Xml创建一个XmlPullParser实例
        XmlPullParser parser = Xml.newPullParser();
        //设置输入流 并指明编码方式
        parser.setInput(is, "UTF-8");
        //触发了第一个事件，根据XML的语法，也就是从他开始了解文档
        int eventType = parser.getEventType();
        //如果获得的事件码如果是文档的结束，那么解析结束
        LogUtils.LogD("开始", "122==============");
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                // 判断当前事件是否为文档开始事件
                case XmlPullParser.START_DOCUMENT:
                    // 初始化ProvinceModel集合
                    provinces = new ArrayList<ProvinceModel>();
                    break;
                // 判断当前事件是否为标签元素开始事件
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("Province")) {
                        province = new ProvinceModel();
                        province.mPId = parser.getAttributeValue(0);
                        province.mProvinceName = parser.getAttributeValue(1);
                        province.isPChecked = false;
                    }
                    break;
                // 标签结束
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("Province")) {
                        provinces.add(province);
                        province = null;
                    }
                    break;
            }
            // 下一个标签
            eventType = parser.next();
        }
        LogUtils.LogD("结束", provinces.size() + "--------" + provinces.toString());
        return provinces;
    }

    @Override
    public String serizlizeP(List<ProvinceModel> provincesS) throws Exception {
        //由android.util.Xml创建一个XmlSerializer实例
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        //设置输出方向为writer
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "Provinces");
        for (ProvinceModel province : provincesS) {
            serializer.startTag("", "Province");
            serializer.attribute("", "ID", province.mPId);
            serializer.attribute("", "ProvinceName", province.mProvinceName);
            serializer.endTag("", "Province");
        }
        serializer.endTag("", "Provinces");
        serializer.endDocument();
        return writer.toString();
    }

    @Override
    public List<CityModel> citys(InputStream is) throws Exception {
        List<CityModel> citys = null;
        CityModel city = null;
        //由android.util.Xml创建一个XmlPullParser实例
        XmlPullParser parser = Xml.newPullParser();
        //设置输入流 并指明编码方式
        parser.setInput(is, "UTF-8");
        //触发了第一个事件，根据XML的语法，也就是从他开始了解文档
        int eventType = parser.getEventType();
        //如果获得的事件码如果是文档的结束，那么解析结束
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                // 判断当前事件是否为文档开始事件
                case XmlPullParser.START_DOCUMENT:
                    // 初始化ProvinceModel集合
                    citys = new ArrayList<CityModel>();
                    break;
                // 判断当前事件是否为标签元素开始事件
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("City")) {
                        city = new CityModel();
                        city.mCId = parser.getAttributeValue(0);
                        city.mCityName = parser.getAttributeValue(1);
                        city.mPID = parser.getAttributeValue(2);
                        city.isCChecked = false;
                    }
                    break;
                // 标签结束
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("City")) {
                        citys.add(city);
                        city = null;
                    }
                    break;
            }
            // 下一个标签
            eventType = parser.next();
        }
        return citys;
    }

    @Override
    public String serizlizeC(List<CityModel> cityS) throws Exception {
        //由android.util.Xml创建一个XmlSerializer实例
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        //设置输出方向为writer
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "Cities");
        for (CityModel city : cityS) {
            serializer.startTag("", "City");
            serializer.attribute("", "ID", city.mCId);
            serializer.attribute("", "CityName", city.mCityName);
            serializer.attribute("", "PID", city.mPID);
            serializer.endTag("", "City");
        }
        serializer.endTag("", "Cities");
        serializer.endDocument();
        return writer.toString();
    }

    @Override
    public List<DistrictsModel> districts(InputStream is) throws Exception {
        List<DistrictsModel> districts = null;
        DistrictsModel district = null;
        //由android.util.Xml创建一个XmlPullParser实例
        XmlPullParser parser = Xml.newPullParser();
        //设置输入流 并指明编码方式
        parser.setInput(is, "UTF-8");
        //触发了第一个事件，根据XML的语法，也就是从他开始了解文档
        int eventType = parser.getEventType();
        //如果获得的事件码如果是文档的结束，那么解析结束
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                // 判断当前事件是否为文档开始事件
                case XmlPullParser.START_DOCUMENT:
                    // 初始化ProvinceModel集合
                    districts = new ArrayList<DistrictsModel>();
                    break;
                // 判断当前事件是否为标签元素开始事件
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("District")) {
                        district = new DistrictsModel();
                        district.mDId = parser.getAttributeValue(0);
                        district.mDistrictsName = parser.getAttributeValue(1);
                        district.mCId = parser.getAttributeValue(2);
                        district.isDChecked = false;
                    }
                    break;
                // 标签结束
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("District")) {
                        districts.add(district);
                        district = null;
                    }
                    break;
            }
            // 下一个标签
            eventType = parser.next();
        }
        return districts;
    }

    @Override
    public String serizlizeD(List<DistrictsModel> DistrictS) throws Exception {
        //由android.util.Xml创建一个XmlSerializer实例
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        //设置输出方向为writer
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "Districts");
        for (DistrictsModel district : DistrictS) {
            serializer.startTag("", "District");
            serializer.attribute("", "ID", district.mDId);
            serializer.attribute("", "DistrictName", district.mDistrictsName);
            serializer.attribute("", "CID", district.mCId);
            serializer.endTag("", "District");
        }
        serializer.endTag("", "Districts");
        serializer.endDocument();
        return writer.toString();
    }
}
